package com.time.workbench.efficient.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.time.workbench.core.util.AuthUtil;
import com.time.workbench.efficient.entity.bo.Application;
import com.time.workbench.efficient.entity.bo.Server;
import com.time.workbench.efficient.entity.dto.ApplicationPageReq;
import com.time.workbench.efficient.entity.dto.ServerPageReq;
import com.time.workbench.efficient.service.ApplicationService;
import com.time.workbench.efficient.service.ServerService;
import com.time.workbench.release.consts.ReleaseConst;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 服务器管理
 *
 * @description: 服务器管理
 * @menu 服务器管理
 */
@RestController
@RequestMapping("/api/efficient/server")
@Slf4j
@RequiredArgsConstructor
public class ServerController {

    public final ServerService serverService;


    /**
     * 根据环境id获取所有服务
     *
     * @param environmentId
     * @return
     */
    @GetMapping("/getByEnvironment")
    public List<Server> getByEnvironment(@RequestParam Integer environmentId) {
        return serverService.getByEnvironment(environmentId);
    }

    /**
     * 获取所有服务器
     *
     * @return
     */
    @GetMapping("/getAll")
    public List<Server> getAll() {
        return serverService.list();
    }

    /**
     * 分页查询
     *
     * @param applicationPageReq
     * @return
     * @status done
     */
    @GetMapping("/page")
    public Page<Server> page(@Validated ServerPageReq applicationPageReq) {
        return serverService.page(applicationPageReq);
    }

    /**
     * 保存服务器
     *
     * @param server
     * @status done
     */
    @PostMapping("")
    public void save(@RequestBody @Validated Server server) {
        server.setTenantId(AuthUtil.getCurrTenantId());
        encryptBase64Password(server);
        serverService.save(server);
    }


    /**
     * 修改服务器
     *
     * @param server
     * @param id
     * @status done
     */
    @PutMapping("/{id}")
    public void update(@RequestBody @Validated Server server, @PathVariable("id") Integer id) {
        server.setId(id);
        encryptBase64Password(server);

        serverService.updateById(server);
    }

    /**
     * 删除服务器
     *
     * @status done
     */
    @DeleteMapping("/{id}")
    public void remove(@PathVariable("id") Integer id) {
        serverService.removeById(id);
    }

    private void encryptBase64Password(Server server) {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, ReleaseConst.ServerKey.getBytes(StandardCharsets.UTF_8));
        server.setPassword(aes.encryptHex(server.getPassword(), CharsetUtil.CHARSET_UTF_8));
    }
}
